/**
 * 
 */
package cn.gov.msa.util;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.stream.FileImageOutputStream;

import com.sun.xml.messaging.saaj.util.Base64;

/**
 * @Title: FileUtil.java
 * @Package cn.gov.msa.util
 * @Description: TODO(用一句话描述该文件做什么)
 * @author 何宇江
 * @date 2018年3月21日 下午3:31:41
 */
public class FileUtil {
	
	public String getFilePath() {
        String path = this.getClass().getResource("/").toString();
        
        if(path.contains("file:/"))
        	path = path.substring(6);
        
        if(path.contains("WEB-INF"))
        	path = path.substring(0, path.length() - 16);
        
        return path;
	}
	
	/**
	 * 数组转换成十六进制字符串
	 * 
	 * @param byte[]
	 * @return HexString
	 */
	public static final String bytesToHexString(byte[] bArray) {
		StringBuffer sb = new StringBuffer(bArray.length);
		String sTemp;
		for (int i = 0; i < bArray.length; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
		}
		return sb.toString();
	}
    /**
     * 十六进制字符串转换成数组
     * 
     * @param bytes
     * @return
     */
    public static byte[] hexStringToBytes(String str) {
        if(str == null || str.trim().equals("")) {
            return new byte[0];
        }

        byte[] bytes = new byte[str.length() / 2];
        for(int i = 0; i < str.length() / 2; i++) {
            String subStr = str.substring(i * 2, i * 2 + 2);
            bytes[i] = (byte) Integer.parseInt(subStr, 16);
        }

        return bytes;
    }
    
	  /** 
     * 根据byte数组，生成文件 
     */  
    public static void getFile(byte[] bfile, String filePath,String fileName) throws Exception {  
        BufferedOutputStream bos = null;  
        FileOutputStream fos = null;  
        File file = null;  
        try {  
            File dir = new File(filePath);  
            if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在  
                dir.mkdirs();  
            }  
            file = new File(filePath+"\\"+fileName);  
            fos = new FileOutputStream(file);  
            bos = new BufferedOutputStream(fos);  
            bos.write(bfile);  
        } catch (Exception e) {  
            e.printStackTrace();  
            throw new Exception(e);
        } finally {  
            if (bos != null) {  
                try {  
                    bos.close();  
                } catch (IOException e1) {  
                    e1.printStackTrace();  
                }  
            }  
            if (fos != null) {  
                try {  
                    fos.close();  
                } catch (IOException e1) {  
                    e1.printStackTrace();  
                }  
            }  
        }  
    }  
    
     
    /**
     * File转换byte[]
     * 
     * @param bytes
     * @return
     */
    public static byte[] fileTobyte(File file)
    {
        byte[] buffer = null;
        try
        {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1)
            {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return buffer;
    }
    
	// byte数组到图片到硬盘上
	public static void svaeFile(byte[] data, String path) {
		if (data.length < 3 || path.equals(""))
			return;// 判断输入的byte是否为空
		try {
			FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));// 打开输入流
			imageOutput.write(data, 0, data.length);// 将byte写入硬盘
			imageOutput.close();
			System.out.println("Make Picture success,Please find image in " + path);
		} catch (Exception ex) {
			System.out.println("Exception: " + ex);
			ex.printStackTrace();
		}
	}
	

	/**
	 * 数组转换成十六进制字符串
	 * 
	 * @param byte[]
	 * @return HexString
	 */
	public static final String bytesBase64(byte[] bArray) {
		StringBuffer sb = new StringBuffer(bArray.length);
		
		byte[] as =Base64.encode(bArray);
		
		return sb.toString();
	}
}
